﻿using System.ComponentModel;
using System.Windows.Input;
using Eirene.Core.Model;
using Microsoft.Practices.Prism.Commands;

namespace Eirene.WPF.ViewModel
{
    /// <summary>
    /// Present a <see cref="Owner"/> object in a form better suited to wpf binding.
    /// </summary>
    /// <remarks>
    /// Using an aggregation; expose-as-needed approach.
    /// </remarks>
    public class OwnerViewModel : INotifyPropertyChanged
    {
        #region Fields
        
        private bool canSetDefaultOwner;

        #endregion

        #region Constructor

        /// <summary>
        /// Initalizes a new instance of the OwnerViewModel class.
        /// </summary>
        /// <param name="data"></param>
        public OwnerViewModel(Owner data)
        {
            Data = data;

            FullName = this.Data.FirstName + " " + this.Data.LastName;

            SetDefaultCommand = new DelegateCommand<object>(
            i => this.Data.IsDefault = true, 
            i => !this.Data.IsDefault);

            CanSetDefaultOwner = !this.Data.IsDefault;
        }

        #endregion

        #region Properties
        
        public bool CanSetDefaultOwner
        {
            get 
            {
                return canSetDefaultOwner;
            }
            set
            {
                if (canSetDefaultOwner != value)
                {
                    canSetDefaultOwner = value;
                    RaisePropertyChanged("CanSetDefaultOwner");
                }
            }
        }
        
        /// <summary>
        /// The original model data.
        /// </summary>
        public Owner Data { get; private set; }

        /// <summary>
        /// The owner's first and last names.
        /// </summary>
        public string FullName { get; private set; }

        #endregion

        #region Commands

        public ICommand SetDefaultCommand { get; private set; }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
